home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / values.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  2.9 KB  |  129 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    values.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include "values.h"
  35.  
  36. #include "alloc.h"
  37. #include "error.h"
  38. #include "list.h"
  39. #include "prim.h"
  40.  
  41. /* primitives */
  42.  
  43. static struct primitive values_prims[] =
  44. {
  45.     {"values", prim_0_rest, values},
  46. };
  47.  
  48. /* function defintions */
  49.  
  50. void
  51. init_values_prims (void)
  52. {
  53.     int num;
  54.  
  55.     num = sizeof (values_prims) / sizeof (struct primitive);
  56.  
  57.     init_prims (num, values_prims);
  58. }
  59.  
  60. Object
  61. make_values (Object vals)
  62. {
  63.     Object obj;
  64.     int i;
  65.  
  66.     if (EMPTYLISTP (vals)) {
  67.     return unspecified_object;
  68.     } else {
  69.     obj = allocate_object (sizeof (struct values));
  70.  
  71.     VALUESTYPE (obj) = Values;
  72.     VALUESNUM (obj) = list_length (vals);
  73.     VALUESELS (obj) = (Object *)
  74.         checking_malloc (VALUESNUM (obj) * sizeof (Object));
  75.  
  76.     for (i = 0; i < VALUESNUM (obj); ++i) {
  77.         VALUESELS (obj)[i] = CAR (vals);
  78.         vals = CDR (vals);
  79.     }
  80.     return (obj);
  81.     }
  82. }
  83.  
  84. Object
  85. construct_values (int num,...)
  86. {
  87.     Object obj;
  88.     int i;
  89.     va_list args;
  90.  
  91.     obj = allocate_object (sizeof (struct values));
  92.  
  93.     VALUESTYPE (obj) = Values;
  94.     VALUESNUM (obj) = num;
  95.     VALUESELS (obj) = (Object *) checking_malloc (num * sizeof (Object));
  96.  
  97.     va_start (args, num);
  98.     for (i = 0; i < num; ++i) {
  99.     VALUESELS (obj)[i] = va_arg (args, Object);
  100.     }
  101.     va_end (args);
  102.     return (obj);
  103. }
  104.  
  105. Object
  106. values (Object rest)
  107. {
  108.     if (EMPTYLISTP (rest) || PAIRP (CDR (rest))) {
  109.     return make_values (rest);
  110.     } else {
  111.     return (CAR (rest));
  112.     }
  113. }
  114.  
  115. Object
  116. devalue (Object val)
  117. {
  118.     if (VALUESP (val)) {
  119.     if (VALUESNUM (val)) {
  120.         return VALUESELS (val)[0];
  121.     } else {
  122.         return error ("Null values construct used in an invalid context",
  123.               NULL);
  124.     }
  125.     } else {
  126.     return val;
  127.     }
  128. }
  129.